﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;

namespace ERP.DataAccess.Util
{
    public enum CompareType
    {
        GreaterThan = 0,
        LessThan = 1,
        Equal = 2,
        GreaterEqualThan = 3,
        LessEqualThan = 4

    }

    public class DALUtil
    {
        public const String strColumnID = "ID";
        public const String strColumnNo = "No";
        public const String strColumnName = "Name";

        public static bool IsValidDataset(DataSet ds)
        {
            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                return true;
            else return false;
        }

        public static DataRow GetFirstRowOnDataset(DataSet ds)
        {
            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                return ds.Tables[0].Rows[0];
            return null;
        }

        public static object GetFirstCellInDataset(DataSet ds)
        {
            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0
                && ds.Tables[0].Rows[0][0] != null && ds.Tables[0].Rows[0][0] != DBNull.Value)
                return ds.Tables[0].Rows[0][0];
            else return null;
        }

        public static String GennerateCondition(String strFieldName, CompareType type, DateTime dt, bool isDateTime)
        {
            /*
             * variable : isDateTime
             *   false : only compare day,month,year (default)
             *   true  : compare day,month,year,hour,minute,second 
            */
            StringBuilder StrBuild = new StringBuilder();
            String strDate = String.Empty;
            String strCompareOperator = String.Empty;
            if (isDateTime)
                strDate = String.Format("'{0:yyyy/M/d HH:mm:ss}'", dt);
            else
            {
                strFieldName = String.Format("(CONVERT(VARCHAR(20), {0}, 112)", strFieldName);
                strDate = String.Format("CONVERT(VARCHAR(20),'{0}',112))", dt.ToString("yyyyMMdd"));
            }


            #region Compare Type
            if (type.Equals(CompareType.Equal))
            {
                strCompareOperator = " = ";
            }
            else if (type.Equals(CompareType.GreaterThan))
            {
                strCompareOperator = " > ";
            }
            else if (type.Equals(CompareType.LessThan))
            {
                strCompareOperator = " < ";
            }
            else if (type.Equals(CompareType.GreaterEqualThan))
            {
                strCompareOperator = " >= ";
            }
            else if (type.Equals(CompareType.LessEqualThan))
            {
                strCompareOperator = " <= ";
            }
            #endregion

            StrBuild.Append(strFieldName);
            StrBuild.Append(strCompareOperator);
            StrBuild.Append(strDate);

            return StrBuild.ToString();
        }

        public static String GenerateBeetween(String strFieldName, DateTime dtFrom, DateTime dtTo, bool isDateTime)
        {
            /*
             * variable : isDateTime
             *   false : only compare day,month,year (default)
             *   true  : compare day,month,year,hour,minute,second 
            */
            StringBuilder StrBuild = new StringBuilder();
            String strDateFrom = String.Empty;
            String strDateTo = String.Empty;

            if (isDateTime)
            {
                strDateFrom = String.Format("'{0:yyyy/M/d HH:mm:ss}'", dtFrom);
                strDateTo = String.Format("'{0:yyyy/M/d HH:mm:ss}'", dtTo);
            }
            else
            {
                strFieldName = String.Format("(CONVERT(VARCHAR(20), {0}, 112)", strFieldName);
                strDateFrom = String.Format("CONVERT(VARCHAR(20),'{0}',112)", dtFrom.ToString("yyyyMMdd"));
                strDateTo = String.Format("CONVERT(VARCHAR(20),'{0}',112))", dtTo.ToString("yyyyMMdd"));
            }

            StrBuild.Append(strFieldName);
            StrBuild.Append(" BETWEEN ");
            StrBuild.Append(strDateFrom);
            StrBuild.Append(" AND ");
            StrBuild.Append(strDateTo);
            return StrBuild.ToString();
        }

        public static String GennerateCondition(String strFieldName, CompareType type, DateTime dt)
        {
            return GennerateCondition(strFieldName, type, dt, false);
        }

        public static String GenerateBeetween(String strFieldName, DateTime dtFrom, DateTime dtTo)
        {
            return GenerateBeetween(strFieldName, dtFrom, dtTo, false);
        }

    }
}
